/*
 * Copyright (c) 2012 - 2013 Johann Hanne
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef _ETHERNET_H
#define _ETHERNET_H

#include <stdlib.h>
#include <stdint.h>
#include <portio.h>
#include <i86.h>
#include <endianness.h>
#include <string.h>

#define ETHERNET_RX_DESCRIPTOR_RING_ADDRESS ((void *)0x00100000)
#define ETHERNET_RX_DATA_BASE_ADDRESS ((void *)0x00200000)

#define ETHERNET_TX_DESCRIPTOR_RING_ADDRESS ((void *)0x00180000)
#define ETHERNET_TX_DATA_BASE_ADDRESS ((void *)0x02000000)

struct ethernet_rx_descriptor {
    uint16_t flags;
    uint16_t rxlen; /* Receive packet length, big endian */
    uint16_t unknown1;
    uint8_t unknown2;
    uint8_t next; /* Offset of next descriptor (counted in 16-byte units, first bit indicates direction) */
    uint32_t unknown3;
    uint32_t dataaddr; /* Physical RAM address of frame data, big endian */
};

struct ethernet_tx_descriptor {
    uint16_t flags;
    uint16_t txlen1; /* Transmit packet length, big endian */
    uint16_t txlen2; /* Transmit packet length, big endian */
    uint8_t unknown1;
    uint8_t next; /* Offset of next descriptor (counted in 16-byte units, first bit indicates direction) */
    uint32_t dataaddr1; /* Physical RAM address of frame data, big endian */
    uint32_t dataaddr2; /* Physical RAM address of frame data, big endian */
};

struct ethernet_tx_element {
    uint8_t *data1;
    size_t len1;
    uint8_t *data2;
    size_t len2;
};

#define ethernet_register_write(reg, value) \
    do { \
        outp(0x900, reg); \
        outp(0x904, value); \
    } while (0)

uint8_t ethernet_register_read(uint8_t reg);
#pragma aux ethernet_register_read = ""  \
    "mov dx, 0x900" \
    "out dx, al" \
    "mov dx, 0x904" \
    "in  al, dx" \
    modify [dx] value [al] parm [al];

void ethernet_set_mac_address(const uint8_t *mac);
void ethernet_get_mac_address(uint8_t *mac);
void ethernet_tx_direct(uint8_t *data1, size_t len1, uint8_t *data2, size_t len2);
void ethernet_tx_direct_vector(struct ethernet_tx_element *elems, size_t numelems);
void ethernet_tx(uint8_t *data, size_t len);
void ethernet_tx_linked_list(void *item, size_t offset_payloadptr, size_t offset_length, size_t offset_nextptr);
void ethernet_rx(void (*rx_cb)(uint8_t *data, size_t len));
int ethernet_init(void);

#endif /* _ETHERNET_H */
